//
//  FeedbackViewController.m
//  Kfangchan
//
//  Created by liran on 14/11/15.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "FeedbackViewController.h"

@interface FeedbackViewController ()

@property (strong, nonatomic) IBOutlet UIView *feedbackView;
@property (strong, nonatomic) IBOutlet UITextView *feedbackTextView;
@property (strong, nonatomic) IBOutlet UILabel *feedbackPlaceHolderLabel;

@property (strong, nonatomic) IBOutlet UIButton *serviceButton;
@property (strong, nonatomic) IBOutlet UILabel *serviceLabel;

@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"意见反馈"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    self.navigationItem.rightBarButtonItem = [[Theam currentTheam] navigationBarRightButtonItemWithImage:nil Title:@"提交" Target:self Selector:@selector(feedback)];
    
    self.feedbackView.layer.borderColor = [UIColor colorWithHexString:@"#D1CCCA"].CGColor;
    self.feedbackView.layer.borderWidth = 1;
    self.feedbackView.layer.cornerRadius = 4.0;
    
    float screenHeight = [UIScreen mainScreen].bounds.size.height;
    self.serviceButton.origin = CGPointMake(self.serviceButton.origin.x, screenHeight - 44 - 50 - self.serviceButton.height - 50);
    self.serviceLabel.origin = CGPointMake(self.serviceLabel.origin.x, self.serviceButton.origin.y + self.serviceButton.height + 20);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark
- (void)feedback {
    if (self.feedbackTextView.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入内容" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    if (self.feedbackTextView.text.length > 500) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"内容限定500字以内" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    NSDictionary *param = @{
                            @"content": self.feedbackTextView.text};
    MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_FEEDBACK_ADD completionHandler:^(id responseObject) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"提交成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        [self back];
    } errorHandler:^(NSError *error) {
        showCustomAlertMessage(@"网路错误");
    } finishHandler:^(id responseObject) {
        
    }];
    [self.opHandlers addObject:operation];
}

- (void)textViewDidChange:(UITextView *)textView {
    if (self.feedbackTextView.text.length == 0) {
        self.feedbackPlaceHolderLabel.text = @"使用过程中，有任何问题，欢迎反馈";
    }else{
        self.feedbackPlaceHolderLabel.text = @"";
    }
}

#pragma mark -
#pragma mark 关闭键盘
- (IBAction)hideKeyboard {
    [self.feedbackTextView resignFirstResponder];
}


#pragma mark -
#pragma mark 
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)call:(id)sender
{
    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"拨打电话:%@", @"4006-999-068"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];
    [alterView show];
    
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        NSString *phone = @"4006999068";
        NSString *tel = [NSString stringWithFormat:@"tel://%@", phone];
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:tel]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
        }
    }
}

- (IBAction)clearText:(id)sender {
    self.feedbackTextView.text = @"";
    self.feedbackPlaceHolderLabel.text = @"使用过程中，有任何问题，欢迎反馈";
}

@end
